package com.qinghe.pro.service.article.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qinghe.pro.Constants;
import com.qinghe.pro.entity.article.Column;
import com.qinghe.pro.exception.ExistedException;
import com.qinghe.pro.exception.ServiceException;
import com.qinghe.pro.mapper.article.ArticleMapper;
import com.qinghe.pro.mapper.article.ColumnMapper;
import com.qinghe.pro.service.article.ColumnService;
import com.qinghe.utils.Utils;

@Service("columnServiceImpl")
public class ColumnServiceImpl implements ColumnService {

    @Autowired
    private ColumnMapper columnMapper;

    @Autowired
    private ArticleMapper articleMapper;

	@Override
	public Column get(Integer id) {
		return columnMapper.selectById(id);
	}

	@Override
	public void insert(Column column) {
		column.setType(Column.STATUS_NORMAL);
		columnMapper.insertSelective(column);
	}

	@Override
	public void update(Column column) {
		columnMapper.updateByPrimaryKeySelective(column);
	}

	@Override
	public void delete(Integer id) {
		Integer type = columnMapper.selectById(id).getType();
		// 幻灯片和右侧新闻栏目不能删除
		if (Constants.COLUMN_TYPE_SLIDER.equals(type) || Constants.COLUMN_TYPE_RIGHT.equals(type)) {
			throw new ServiceException("该栏目属于首页栏目，不能删除。");
		}
		if (articleMapper.selectByColumnId(id).size() > 0) {
			throw new ExistedException("该栏目下有新闻数据，不能删除。");
		}
		columnMapper.deleteByPrimaryKey(id);
	}

	@Override
	public String findMainAsTree() {
		List<Column> columnList = columnMapper.selectAll();

		return makeTree(columnList, false);
	}

	@Override
	public String findAllAsTree() {
		List<Column> columnList = columnMapper.selectAll();

		return makeTree(columnList, true);
	}

	/**
	 * 生成下拉树的json字符串
	 * 
	 * @param list
	 * @param hasRoot true:有根节点；false：无根节点
	 * 
	 * @return json
	 * @author longfei
	 */
	private String makeTree(List<Column> list, boolean hasRoot) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for(Column column : list) {
			if (!hasRoot && column.getParent() == null) {
				continue;
			}
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("id", column.getId());
			item.put("name", column.getName());
			if (column.getParent() == null) {
				item.put("pId", 0);
			} else {
				item.put("pId", column.getParent().getId());
			}
			
			result.add(item);
		}
		
		return Utils.JsonUtil.toJson(result);
	}

	@Override
	public Column getTree() {
		List<Column> list = columnMapper.selectByTypeNot(Column.STATUS_SLIDER);

		List<Column> rootList = makeTree(list);

		return rootList.get(0);
	}

	private List<Column> makeTree(List<Column> list) {
		List<Column> parent = new ArrayList<Column>();
		// get parentId = null;
		for (Column e : list) {
			if (e.getParent() == null) {
				e.setChildren(new ArrayList<Column>(0));
				parent.add(e);
			}
		}
		// 删除parentId = null;
		list.removeAll(parent);

		makeChildren(parent, list);

		return parent;
	}

	private void makeChildren(List<Column> parent, List<Column> children) {
		if (children.isEmpty()) {
			return;
		}

		List<Column> tmp = new ArrayList<Column>();
		for (Column c1 : parent) {
			for (Column c2 : children) {
				c2.setChildren(new ArrayList<Column>(0));
				if (c1.getId().equals(c2.getParent().getId())) {
					c1.getChildren().add(c2);
					tmp.add(c2);
				}
			}
		}

		children.removeAll(tmp);

		makeChildren(tmp, children);
	}

}
